package com.ds.home.database;

import com.alibaba.fastjson.JSONObject;
import com.ds.home.EIArea;
import com.ds.home.EIPlace;
import com.ds.home.EIZNode;
import com.ds.home.database.vo.PlaceVO;
import com.ds.iot.enums.DeviceStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Place entity.
 *
 * @author MyEclipse Persistence Tools
 */

public class DBPlace implements EIPlace {

    private PlaceVO vo;

    public List<String> znodeIds=new ArrayList<String>();


    public PlaceVO getVo() {
        return vo;
    }

    public void setVo(PlaceVO vo) {
        this.vo = vo;
    }

    protected DBPlace(PlaceVO vo) {
        this.vo = vo;
    }

    public String getName() {
        return vo.getName();
    }

    public String getPlaceid() {
        return vo.getPlaceid();
    }

    public String getUserid() {
        return vo.getUserid();
    }

    public void setMemo(String memo) {
        vo.setMemo(memo);
    }

    public void setName(String name) {
        vo.setName(name);
    }

    public void setPlaceid(String placeid) {
        vo.setPlaceid(placeid);
    }

    public void setStart(String start) {
        vo.setStart(start);
    }

    public void setUserid(String userid) {
        vo.setUserid(userid);
    }

    public String getMemo() {
        return vo.getMemo();
    }

    public String getStart() {
        return vo.getStart();
    }

    @Override
    public void setOrgid(String orgid) {
        vo.setOrgid(orgid);
    }

    @Override
    public Set<String> getChildPlaceIds() {
        return MEMManagerImpl.getInstance().getChildPlaceIds(this.getPlaceid());
    }

    @Override
    public List<EIPlace> getChildPlaces() {
        return MEMManagerImpl.getInstance().getChildPlaces(this.getPlaceid());
    }

    @Override
    public String getParentId() {
        return vo.getParentid();
    }

    @Override
    public void setParentId(String parentId) {
        vo.setParentid(parentId);
    }

    public List<EIArea> getAreas() {
        List<EIArea> areas = new ArrayList<EIArea>();
        Set<String> areaIds = this.getAreaIds();
        for (String areaid : areaIds) {
            EIArea area = MEMManagerImpl.getInstance().getAreaById(areaid);
            if (area != null) {
                areas.add(area);
            }

        }
        return areas;

    }

    public EIArea addArea(EIArea area) {
        Set<String> areaIds = this.getAreaIds();
        if (!areaIds.contains(area.getAreaid())) {
            areaIds.add(area.getAreaid());
            MEMManagerImpl.getInstance().getAllAreaIdCache().put(this.getPlaceid(), JSONObject.toJSONString(areaIds));
        }
        return area;
    }

    public EIPlace addChild(EIPlace palce) {
        Set<String> placeIds = this.getChildPlaceIds();
        if (!placeIds.contains(palce.getPlaceid())) {
            placeIds.add(palce.getPlaceid());
            MEMManagerImpl.getInstance().getPlaceRefChildIdCache().put(this.getPlaceid(), JSONObject.toJSONString(placeIds));
        }
        return palce;
    }

    public void removeChild(String placeId) {
        Set<String> placeIds = this.getChildPlaceIds();
        if (!placeIds.contains(placeId)) {
            placeIds.remove(placeId);
            MEMManagerImpl.getInstance().getPlaceRefChildIdCache().put(this.getPlaceid(), JSONObject.toJSONString(placeIds));
        }
    }

    public void removeArea(String endPointId) {
        Set<String> areaIds = this.getAreaIds();
        if (areaIds.contains(endPointId)) {
            areaIds.remove(endPointId);
            MEMManagerImpl.getInstance().getAllAreaIdCache().put(this.getPlaceid(), JSONObject.toJSONString(areaIds));
        }
    }

    public Set<String> getAreaIds() {
        Set<String> areaIdList = MEMManagerImpl.getInstance().getAreaIdsByPlaceId(this.getPlaceid());
        return areaIdList;
    }



    @Override
    public String getOrgid() {
        return vo.getOrgid();
    }

}